import api from "../api";
import {
	info
} from '@/api/user.js';

function parseQueryString(queryString) {
	const result = {};
	const reg = /([^?&]+)=([^?&]+)/g;
	let match;

	while ((match = reg.exec(queryString))) {
		result[decodeURIComponent(match[1])] = decodeURIComponent(match[2]);
	}

	return result;
}


export const openPddEvent = async () => {
	const res = await api.pddApi.toPdd()
	plus.runtime.openURL(encodeURI(res.data.link?.we_app_web_view_url || res.data[0].url))
}
// #ifdef APP
const Alibcsdk = uni.requireNativePlugin('UZK-Alibcsdk');
Alibcsdk.init(
	result => {
		console.log('阿里',JSON.stringify(result))
	}
)
const plug = uni.requireNativePlugin('UZK-JDopen');
plug.init(result => {
	console.log('京东',JSON.stringify(result));
});
// #endif


export const openJDList = async () => {
	const res = await api.jdApi.link()
	console.log(res.data.shortURL);
	
	plug.openurl({
		url: res.data.shortURL
	}, result => {  
		if(!result.status){
			uni.showToast({
				icon:'none',
				title:result.msg
			})
		}
	});
}


// 换取备案渠道ID
const setBeian = async (e) => {
	console.log(e);
	const res = await api.tmApi.publisher({
		access_token: e.access_token
	})
	console.log('resresresresresresresresresres',res);
	if (res.status == 200) {
		uni.showToast({
			icon: 'none',
			title: '备案成功'
		})
	}
}
// 处理没有 渠道id
const handlerNoId = () => {
	Alibcsdk.getpublisher({
			url: 'https://oauth.taobao.com/authorize?response_type=token&client_id=34819607&redirect_uri=https://app.minggoudao.com&state=1212&view=web'
		},
		result => {
			if (result.status) {
				// 备案授权成功处理
				console.log(3333)
				setBeian(result.data)
			} else {

				// 备案授权失败处理
				if (result.msg == '请先调用login方法' || result.data == '请先调用login方法') {
					console.log(222)
					console.log(result)
					Alibcsdk.login(
						res => {
							console.log(res);
							if (res.status) {

							} else {
								console.log(res)
								uni.showToast({
									icon: 'none',
									title: '淘宝授权失败'
								})
							}
						}
					)
				} else {
					console.log(333)
					console.log(result)
					uni.showToast({
						icon: 'none',
						title: '用户取消备案授权'
					})
				}
			}
		})
}


const goTm = async () => {
	const res = await api.tmApi.getSortLink()
	const url = res.data.data.click_url
	Alibcsdk.openurl({
		url: url,
		linkkey: "tmall",
	})
}
const goTaoBao = async () => {
	const res = await api.tmApi.getTBLink()
	const url = res.data.data.click_url
	Alibcsdk.openurl({
		url: url,
	})
}


export const openTMEvent = async (opt) => {
	const res = await info()
	console.log('userInfouserInfo',res.data.userInfo);
	const relation_id = res.data.userInfo.relation_id
	if (!relation_id) {
		handlerNoId()
	} else {
		opt == 'tm' ? goTm() : goTaoBao()
	}
}


export const handleWph = async () => {
	const isHaveMt = plus.runtime.isApplicationExist({
		pname: 'com.achievo.vipshop'
	})
	if (!isHaveMt) {
		uni.showToast({
			title: '请先下载唯品会APP',
			icon: 'none'
		})
		return
	}
	const res = await api.wphApi.getSortLink()
	console.log(res.data.deeplinkUrl);
	plus.runtime.openURL(res.data.deeplinkUrl)
}

export const handleTuanGou = async () => {
	const isHaveMt = plus.runtime.isApplicationExist({
		pname: 'com.sankuai.meituan'
	})
	if (!isHaveMt) {
		uni.showToast({
			title: '请先下载美团APP',
			icon: 'none'
		})
		return
	}
	const res = await api.mtwmApi.getTuangou()
	console.log(res)
	plus.runtime.openURL(res.data.link, () => {
		// uni.showToast({
		// 	title: '未找到打开此链接的APP',
		// 	icon: 'none'
		// })
	})
}

export const handleMingSu = async () => {
	const isHaveMt = plus.runtime.isApplicationExist({
		pname: 'com.sankuai.meituan'
	})
	if (!isHaveMt) {
		uni.showToast({
			title: '请先下载美团APP',
			icon: 'none'
		})
		return
	}

	const res = await api.mtwmApi.getMingSu()
	plus.runtime.openURL(res.data.link, () => {
		// uni.showToast({
		// 	title: '未找到打开此链接的APP',
		// 	icon: 'none'
		// })
	})
}